package com.example.takeaway.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.takeaway.common.ResponseEnum;
import com.example.takeaway.constant.CacheNames;
import com.example.takeaway.exception.BusException;
import com.example.takeaway.mapper.UserAccountMapper;
import com.example.takeaway.mapper.UserMapper;
import com.example.takeaway.model.bo.User;
import com.example.takeaway.model.bo.UserAccount;
import com.example.takeaway.model.dto.ShopLoginDTO;
import com.example.takeaway.model.vo.UserTokenVO;
import com.example.takeaway.service.UserService;
import com.example.takeaway.util.MailSender;
import com.example.takeaway.util.RedisUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-10-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    StringRedisTemplate stringRedisTemplate;

    @Resource
    UserMapper userMapper;

    @Resource
    UserAccountMapper userAccountMapper;

    @Override
    public void sendCode(String email) {
        int code = RandomUtil.randomInt(100000, 999999);
        RedisUtil.set(CacheNames.LOGIN_EMAIL_CODE + email, Integer.toString(code), 5, TimeUnit.MINUTES);
        MailSender.sendCode(email, Integer.toString(code));
    }

    @Override
    public UserTokenVO loginByCode(String email, String code) {
        User user = userMapper.selectByEmail(email);
        if (user == null) {
            throw new BusException(ResponseEnum.USER_NOT_EXIST);
        }
        String cacheCode = RedisUtil.get(CacheNames.LOGIN_EMAIL_CODE + email);
        if (code.equals(cacheCode)) {
            //删除redis中的验证码
            RedisUtil.delete(CacheNames.LOGIN_EMAIL_CODE + email);
            //生成token,并讲用户的信息存入redis,返回给前端
            String token = IdUtil.fastSimpleUUID();
            RedisUtil.set(CacheNames.LOGIN_USER_INFO + token, JSON.toJSONString(user), 7, TimeUnit.DAYS);
            UserTokenVO userTokenVO = new UserTokenVO();
            userTokenVO.setToken(token);
            userTokenVO.setExpireTime(7 * 24 * 60 * 60L);
            return userTokenVO;
        }
        throw new BusException(ResponseEnum.CODE_ERROR);
    }

    @Override
    public void sendAdminCode(String email) {
        int code = RandomUtil.randomInt(100000, 999999);
        RedisUtil.set(CacheNames.LOGIN_EMAIL_CODE + email, Integer.toString(code), 5, TimeUnit.MINUTES);
        MailSender.sendCode(email, Integer.toString(code));
    }

    @Override
    public UserTokenVO adminLoginByCode(String email, String code) {
        UserAccount account = userAccountMapper.selectByEmail(email);
        if (account == null) {
            throw new BusException(ResponseEnum.USER_NOT_EXIST);
        }
        String cacheCode = RedisUtil.get(CacheNames.LOGIN_EMAIL_CODE + email);
        if (code.equals(cacheCode)) {
            //删除redis中的验证码
            RedisUtil.delete(CacheNames.LOGIN_EMAIL_CODE + email);
            //生成token,并讲用户的信息存入redis,返回给前端
            String token = IdUtil.fastSimpleUUID();
            RedisUtil.set(CacheNames.LOGIN_USER_INFO + token, JSON.toJSONString(account), 7, TimeUnit.DAYS);
            UserTokenVO userTokenVO = new UserTokenVO();
            userTokenVO.setToken(token);
            userTokenVO.setExpireTime(7 * 24 * 60 * 60L);
            return userTokenVO;
        }
        throw new BusException(ResponseEnum.CODE_ERROR);
    }

    @Override
    public UserTokenVO loginByUsernameAndPassword(ShopLoginDTO param) {
        UserAccount account = userAccountMapper.selectByUserName(param.getUsername());
        if (account == null) {
            throw new BusException(ResponseEnum.USER_NOT_EXIST);
        }
        if (!account.getPassword().equals(param.getPassword())) {
            throw new BusException(ResponseEnum.CODE_ERROR);
        }
        String token = IdUtil.fastSimpleUUID();
        //先删旧token
        String oldToken = RedisUtil.get(CacheNames.LOGIN_USER_TOKEN + account.getUserId());
        if (oldToken != null) {
            RedisUtil.delete(CacheNames.LOGIN_USER_INFO + oldToken);
            RedisUtil.delete(CacheNames.LOGIN_USER_TOKEN + account.getUserId());
        }
        RedisUtil.set(CacheNames.LOGIN_USER_INFO + token, JSON.toJSONString(account), 7, TimeUnit.DAYS);
        RedisUtil.set(CacheNames.LOGIN_USER_TOKEN + account.getUserId(), token, 7, TimeUnit.DAYS);
        UserTokenVO userTokenVO = new UserTokenVO();
        userTokenVO.setToken(token);
        userTokenVO.setExpireTime(7 * 24 * 60 * 60L);
        return userTokenVO;
    }
}
